package chkui.springcore.example.javabase.conversion;

import java.beans.IntrospectionException;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;

import chkui.springcore.example.common._G;
import chkui.springcore.example.javabase.conversion.entity.PC;
import chkui.springcore.example.javabase.conversion.entity.Phone;
import chkui.springcore.example.javabase.conversion.support.PC2StringConverter;
import chkui.springcore.example.javabase.conversion.support.String2DeviceConverterFactory;
import chkui.springcore.example.javabase.conversion.support.String2PcConverter;

public class ConversionApp {

	public static void main(String[] args) throws IntrospectionException {
		new ConversionApp().singletonConversion().factoryConversion().registConversionService();
	}

	ConversionApp singletonConversion() {
		final String text = "cpu=amd;ram=kingston;graphic=Navidia;";
		Converter<String, PC> string2Pc = new String2PcConverter();
		PC pc = string2Pc.convert(text);
		_G.print("String to Pc:");
		_G.print("'", text, "' ==> '", pc, "'");

		Converter<PC, String> pc2String = new PC2StringConverter();
		String string = pc2String.convert(pc);
		_G.print("Pc to String:");
		_G.print("'", pc, "' ==> '", string, "'");

		return this;
	}

	ConversionApp factoryConversion() {
		String2DeviceConverterFactory factory = new String2DeviceConverterFactory();
		Converter<String, PC> pcConverter = factory.getConverter(PC.class);
		// 将字符串转换为PC
		PC pc = pcConverter.convert("cpu=amd;ram=kingston;graphic=Navidia;");
		_G.print(pc);

		Converter<String, Phone> phoneConverter = factory.getConverter(Phone.class);
		// 将字符串转换为Phone
		Phone phone = phoneConverter.convert("name=HUAWEIP20;cpu=Kirin970;ram=64G;");
		_G.print(phone);
		return this;
	}

	ConversionApp registConversionService() {
		ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConversionConfig.class);
		ConversionService service = ctx.getBean(ConversionService.class);
		// 字符串转换为整型
		int i = service.convert("123456", Integer.class);
		_G.print(i);
		// 字符串转换为浮点
		float f = service.convert("1234.56", Float.class);
		_G.print(f);
		
		// 源生列表转换为List
		List<?> list = service.convert(new int[] { 1, 2, 3, 4, 5, 6 }, List.class);
		_G.print(list);

		// 源生列表转换为Set
		Set<?> set = service.convert(new int[] { 1, 2, 3, 4, 5, 6 }, Set.class);
		_G.print(set);
		
		// 枚举转换
		Gender gender = service.convert("Male", Gender.class);
		_G.print(gender);
		
		// 使用自定义转换器
		PC pc = service.convert("cpu=amd;ram=kingston;graphic=Navidia;", PC.class);
		_G.print(pc);
		
		// UUID转换
		UUID uuid = service.convert("f51b4b95-0925-4ad0-8c62-4daf3ea7918f", UUID.class);
		_G.print(uuid);
		
		// 字符串转换为Optional<PC>
		Optional<PC> options = service.convert("cpu=amd;ram=kingston;graphic=Navidia;", Optional.class);
		_G.print(options);
		
		// 使用TypeDescriptor描述进行转换
		String source = "123456789";
		int result = (int) service.convert(source, TypeDescriptor.valueOf(source.getClass()),
				TypeDescriptor.valueOf(Integer.class));
		_G.print(result);
		ctx.close();
		return this;
	}

	enum Gender {
		Male, Female, Other
	}
}
